﻿using UnityEngine;
using System.Collections;
using UnityEngine.UI;

namespace Game
{
    
	public class DebugHelper : MonoBehaviour 
	{
        /// <summary>
        /// 每次更新FPS的时间间隔
        /// </summary>
        [SerializeField]float m_updateDeltaTime = 0.3f;

        [SerializeField]Text m_fpsText;
        float m_timeToCount = 0.3f;

        [SerializeField] Light m_lightToControll;
        [SerializeField] Toggle m_toggleShadow;
        [SerializeField] Slider m_sliderCameraViewRange;
        [SerializeField] Text m_textCameraViewRange;
        private void Start()
        {
            if (m_fpsText == null)
            {
                Debug.LogError("No text for FPS found");
                m_fpsText = this.GetComponent<Text>();
            }

            if (m_toggleShadow)
            {
                //阴影设置：
                if (m_lightToControll == null)
                {
                    m_toggleShadow.gameObject.SetActive(false);
                }
                else
                {
                    m_toggleShadow.onValueChanged.AddListener(
                        (isOn) =>
                        {
                            if (isOn)
                            {
                                m_lightToControll.shadows = LightShadows.Hard;
                            }
                            else
                            {
                                m_lightToControll.shadows = LightShadows.None;
                            }
                        });

                    //--是否阴影的默认值
                    m_toggleShadow.isOn = m_lightToControll.shadows != LightShadows.None;
                }
            }

            if (m_sliderCameraViewRange)
            {
                Camera main = Camera.main;
                m_sliderCameraViewRange.value = main.farClipPlane;
                m_sliderCameraViewRange.minValue = 100;
                m_sliderCameraViewRange.maxValue = 1000;
                m_sliderCameraViewRange.onValueChanged.AddListener(
                    (val) =>
                    {
                        main.farClipPlane = val;
                        m_textCameraViewRange.text = "Cam: " + val;
                    });

                //--镜头范围
                m_sliderCameraViewRange.value = main.farClipPlane;
            }
        }

        
		void Update () 
		{
            m_timeToCount -= Time.deltaTime;
            if(m_timeToCount<=0)
            {
                m_timeToCount = m_updateDeltaTime;
                m_fpsText.text = ((int)(1f/Time.deltaTime)).ToString();
            }
		}

		//---------------------------------------------------------------------------
		//				Public area
		//---------------------------------------------------------------------------
		#region Public area
		#endregion

		//---------------------------------------------------------------------------
		//				Private area
		//---------------------------------------------------------------------------
		#region Private area
		#endregion
	}
}